#include "Periferial.h"
#include "ScreenServer.h"

unsigned long g_ulFlags = 0;
unsigned long g_ulWheel = 0;
unsigned long g_ulADCData = 0;
unsigned long g_ulScore = 0;

unsigned short g_puhLevel[5] = { 0, 20, 50, 150, 300 };

unsigned long g_ulTimer_count = 0;
unsigned long g_ulLast_click = 0;
unsigned long g_ulTimer2Count=0;
//use this counter for screen movement
int icounter=0;

//*****************************************************************************
//
// The interrupt handler for the ADC interrupt.
//
//*****************************************************************************
void
ADCIntHandler(void)
{
    //
    // Clear the ADC interrupt.
    //
    ADCIntClear(ADC_BASE, 3);
    //
    // Read the data from the ADC.
    //
    ADCSequenceDataGet(ADC_BASE, 3, &g_ulWheel);
    //
    // Pass the ADC data through the filter
    //
	if( g_ulWheel > (g_ulADCData + 51) || g_ulWheel < (g_ulADCData - 51) )
		g_ulADCData = g_ulWheel;
		
	HWREGBITW(&g_ulFlags, FLAG_WORKER ) = 1;
	if (icounter<95)
	{
		icounter++;			
	}
	else
	{
	 	icounter=0;
	}
}
Timer2IntHandler(void)
{
	TimerIntClear(TIMER2_BASE,TIMER_B);
	g_ulTimer2Count++;			
}
void
Timer0IntHandler(void)
{
	TimerIntClear(TIMER0_BASE,TIMER_B);
	g_ulTimer_count++;
	//proverqva dali predi poveche ot 200 mili sekundi e imalo natisnat buton
	if(g_ulTimer_count - g_ulLast_click > 200 && (HWREGBITW(&g_ulFlags, FLAG_BUTTONSTATE ) == 1))
	{
		//imalo e znachi imame edno clickvane i kazvame na worker-a che e 
		//edno a systoqnieto na butona e "nqma click otdavna"
		HWREGBITW(&g_ulFlags, FLAG_CLICK ) = 1;
		HWREGBITW(&g_ulFlags, FLAG_BUTTONSTATE ) = 0;
	}		
}

void
ButtonIntHandler(void)
{
	GPIOPinIntClear(GPIO_PORTC_BASE, PUSH_BUTTON);
	//ako imame flag click 4akame da bude obraboten ot programata predi da vzimame novite zaqvki
	if( HWREGBITW(&g_ulFlags, FLAG_CLICK ) == 1 )
		return;
	//mnogo byrz click < 25ms e shum
 	if(g_ulTimer_count - g_ulLast_click < 25 && ( (HWREGBITW(&g_ulFlags, FLAG_BUTTONSTATE ) == 1 ) ||
												  HWREGBITW(&g_ulFlags, FLAG_PAUSE ) == 1) )
 		return;
 	//imame natisnat buton predi po malko ot 200 ms?
 	if(g_ulTimer_count - g_ulLast_click < 200 && (HWREGBITW(&g_ulFlags, FLAG_BUTTONSTATE ) == 1))
 	{
  		//nqmame natisnat buton veche ama imame double click
  		HWREGBITW(&g_ulFlags, FLAG_BUTTONSTATE ) = 0;
  		HWREGBITW(&g_ulFlags, FLAG_PAUSE ) = 1;
		g_ulLast_click = g_ulTimer_count;
 	}

 	else
 	{
  		//imame natisnat buton v momenta i chakame 200ms (v timer-a) da kajem che imame edin natisnat buton ili
  		//ako dojde vtoro natiskane znachi imame double click
  		g_ulLast_click = g_ulTimer_count;
  		HWREGBITW(&g_ulFlags, FLAG_BUTTONSTATE ) = 1; 
 	}	
}

int main(void)
{
	unsigned char ucLevelCount = 1;

	//
	Periferial();
	//Turn on Screen Server
	ScreenServer();
	//Loop Forever
	//
	while(1)
	{
		if( g_ulScore > g_puhLevel[ucLevelCount] && ucLevelCount < 5 )
		{
			TimerLoadSet(TIMER1_BASE, TIMER_A, SysCtlClockGet() / (8+ucLevelCount) );
			ucLevelCount++;
		}
			 
		if( HWREGBITW(&g_ulFlags, FLAG_GAMEOVER ) == 1 )
		 {
			g_ulTimer2Count=0;
			while(HWREGBITW(&g_ulFlags, FLAG_CLICK ) == 0)
			{
			 	g_ulTimer2Count++;
				if(g_ulTimer2Count>20000000)
				{
				   break;
				}
			}
			ScreenServer();
			ClearTetrisMatrix();
			HWREGBITW(&g_ulFlags, FLAG_GAMEOVER ) = 0;
			HWREGBITW(&g_ulFlags, FLAG_CLICK ) = 0;
		}
		else if( HWREGBITW(&g_ulFlags, FLAG_PAUSE ) == 1 ) {
			while( HWREGBITW(&g_ulFlags, FLAG_CLICK ) == 0 ){}
			HWREGBITW(&g_ulFlags, FLAG_CLICK ) = 0;
			HWREGBITW(&g_ulFlags, FLAG_PAUSE ) = 0;
		}	 
		else		
			Worker();
	}
		 
		
}
